<template>
	<a-row>
		<a-col :span="5">
			<s-tree
				class="cardImp"
				placeholder="Search"
				:loading="cardLoading"
				:expandedKeys="defaultExpandedKeys"
				:data="treeData"
				:fieldNames="treeFieldNames"
				@select="treeSelect"
			></s-tree>
		</a-col>
		<a-col :span="19">
			<a-card :bordered="false" style="margin-bottom: 10px">
				<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
					<a-row :gutter="24">
						<a-col :span="8">
							<a-form-item name="searchKey" label="名称关键词">
								<a-input
									v-model:value="searchFormState.searchKey"
									placeholder="请输入组织名称关键词"
									@keyup.enter="table.refresh(true)"
								></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-button type="primary" @click="table.refresh(true)">
								<template #icon>
									<SearchOutlined />
								</template>
								查询
							</a-button>
							<a-button class="buttom-left" @click="() => searchFormRef.resetFields()">
								<template #icon>
									<redo-outlined />
								</template>
								重置
							</a-button>
						</a-col>
					</a-row>
				</a-form>
			</a-card>
			<a-card :bordered="false">
				<s-table
					ref="table"
					table-id="SYS_ORG_TABLE"
					:columns="columns"
					:data="loadData"
					:expand-row-by-click="true"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:row-selection="options.rowSelection"
					:tool-config="toolConfig"
				>
					<template #operator>
						<a-space>
							<a-button type="primary" @click="formRef.onOpen(null, searchFormState.parentId)">
								<template #icon>
									<plus-outlined />
								</template>
								新增组织
							</a-button>
							<a-button type="primary" :disabled="selectedRowKeys.length === 0" @click="copy.onOpen(selectedRowKeys)">
								<template #icon>
									<copy-outlined />
								</template>
								复制组织
							</a-button>
							<batch-delete
								title="删除选中组织与下级组织吗?"
								buttonName="删除组织"
								:selectedRowKeys="selectedRowKeys"
								@batchDelete="deleteBatchOrg"
							/>
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column?.dataIndex === 'category'">
							{{ $TOOL.dictTypeData('ORG_CATEGORY', record.category) }}
						</template>
						<template v-if="column?.dataIndex === 'action'">
							<a @click="formRef.onOpen(record)">编辑</a>
							<a-divider type="vertical" />
							<a-popconfirm title="删除此组织与下级组织吗？" @confirm="removeOrg(record)">
								<a-button type="link" danger size="small">删除</a-button>
							</a-popconfirm>
						</template>
					</template>
				</s-table>
			</a-card>
		</a-col>
	</a-row>
	<Form ref="formRef" @successful="table.refresh()" />
	<Copy ref="copy" @successful="table.clearRefreshSelected()" />
</template>

<script setup name="sysOrg">
	import { message, Empty } from 'ant-design-vue'
	import orgApi from '@/api/sys/orgApi'
	import Form from './form.vue'
	import Copy from './copy.vue'
	import { isEmpty } from 'lodash-es'

	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false } //表格工具配置

	const columns = [
		{ title: '组织名称', dataIndex: 'name' },
		{ title: '分类', dataIndex: 'category' },
		{ title: '排序', dataIndex: 'sortCode', width: 100 },
		{ title: '操作', dataIndex: 'action', align: 'center', width: '150px' }
	]
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = reactive({
		alert: {
			show: true, //显示选择多少个
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	})
	// 定义tableDOM
	const table = ref(null)
	const formRef = ref()
	const copy = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	// 默认展开的节点
	const defaultExpandedKeys = ref([])
	const treeData = ref([])
	// 替换treeNode 中 title,key,children
	const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
	const cardLoading = ref(true)
	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		loadTreeData()
		return orgApi.orgPage(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}
	// 加载左侧的树
	const loadTreeData = () => {
		orgApi.orgTree().then((res) => {
			cardLoading.value = false
			if (res !== null) {
				treeData.value = res
				if (isEmpty(defaultExpandedKeys.value)) {
					// 默认展开2级
					treeData.value.forEach((item) => {
						// 因为0的顶级
						if (item.parentId === 0) {
							defaultExpandedKeys.value.push(item.id)
							// 取到下级ID
							if (item.children) {
								item.children.forEach((items) => {
									defaultExpandedKeys.value.push(items.id)
								})
							}
						}
					})
				}
			}
		})
	}
	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys.length > 0) {
			searchFormState.value.parentId = selectedKeys.toString()
		} else {
			delete searchFormState.value.parentId
		}
		table.value.refresh(true)
	}
	// 删除
	const removeOrg = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		orgApi.orgDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchOrg = (params, callback) => {
		orgApi
			.orgDelete(params)
			.then(() => {})
			.finally(() => {
				table.value.clearRefreshSelected()
				callback() //调用组件中的回调方法
			})
	}
</script>

<style scoped>
	.cardImp {
		margin-right: 10px;
	}

	.ant-form-item {
		margin-bottom: 0 !important;
	}

	.primaryAdd {
		margin-right: 10px;
	}

	.buttom-left {
		margin-left: 8px;
	}
</style>
